package org.xtext.runtime.library;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.xtext.runtime.formula.Predicate;
import org.xtext.runtime.interpreter.Agent;
import org.xtext.runtime.interpreter.PredicateBuilder;
import org.xtext.runtime.term.ITerm;
import org.xtext.runtime.term.Variable;

public class Library {
	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.METHOD)

	public @interface ACTION {}

	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.METHOD)
	public @interface SENSOR {}

	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.METHOD)
	public @interface TERM {}

	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.METHOD)
	public @interface FORMULA {}

	protected Agent agent;
	
	public Library() {
	}
	
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	
	protected Predicate createPredicate(String name, ITerm[] terms) {
		PredicateBuilder builder = agent.getPredicateBuilder(name);
		if (builder == null) java.lang.System.out.println("failed to get builder: " + name);
		if (builder.getVariables() == null) {
			java.lang.System.out.println("no variables for: " + name);
		} else {
			int i=0;
			for (Variable variable : builder.getVariables()) {
				builder.addBinding(variable, terms[i++]);
			}
		}
		return builder.getGroundPredicate();
	}
}
